package com.tt.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.tt.bean.GroupBean;
import com.tt.utils.GetResult;
@Repository
public class GroupDaoImpl implements GroupDao {
	
	@Override
	public GroupBean createGroup(GroupBean groupBean) {
		GroupBean one = new GroupBean();
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			Long id = ss.selectOne("com.tt.bean.GroupFriend.selectMaxID");
			one.setId(id);
			groupBean.setId(id);
			ss.insert("com.tt.bean.GroupFriend.createGroup", groupBean);
			ss.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		return one;
	}

	@Override
	public void insertGroup(GroupBean groupBean) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.insert("com.tt.bean.GroupFriend.insertFriend", groupBean);
			ss.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		
	}

	@Override
	public List<GroupBean> selectGroupList(GroupBean groupBean) {
		SqlSession ss = null;
		List<GroupBean> list = new ArrayList<>(); 
		try {
			ss = GetResult.getSqlSession();
			list = ss.selectList("com.tt.bean.GroupFriend.selectGroupList", groupBean);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		return list;
	}

	@Override
	public void requestGroupFriend(GroupBean groupBean) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.insert("com.tt.bean.GroupFriend.requestGroupFriend", groupBean);
			ss.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
	}

	@Override
	public List<GroupBean> requestGroupMessage(GroupBean groupBean) {
		SqlSession ss = null;
		List<GroupBean> list = new ArrayList<>(); 
		try {
			ss = GetResult.getSqlSession();
			list = ss.selectList("com.tt.bean.GroupFriend.requestGroupMessage", groupBean);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		return list;
	}

	@Override
	public void agreeGroup(GroupBean groupBean) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.update("com.tt.bean.GroupFriend.agreeGroup", groupBean);
			ss.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
	}

	@Override
	public void refuseGroup(GroupBean groupBean) {
		SqlSession ss = null;
		try {
			ss = GetResult.getSqlSession();
			ss.delete("com.tt.bean.GroupFriend.refuseGroup", groupBean);
			ss.commit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		
	}

	@Override
	public List<GroupBean> selectGroupFriend(Long id, Long uid) {
		SqlSession ss = null;
		HashMap<String, Long> hm = new HashMap<>();
		hm.put("id", id);
		hm.put("uid", uid);
		List<GroupBean> list = new ArrayList<>();
		try {
			ss = GetResult.getSqlSession();
			list = ss.selectList("com.tt.bean.GroupFriend.selectGroupFriend", hm);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			ss.close();
		}
		return list;
	}

	

}
